package com.lu.adog.util.image;

import android.graphics.Bitmap;

import com.lu.adog.util.Logg;

/**
 * @author Yue
 * @date 2017/11/17 17:42
 */

public class PSGray {


    /**
     * Photoshop 黑白算法，默认效果
     *
     * @param image
     * @return 新的黑白化图片
     */
    public static Bitmap createBlackWhiteImage(Bitmap image) {
        return createBlackWhiteImage(image, null);
    }

    /**
     * Photoshop 黑白算法，默认效果
     *
     * @param image
     * @return 新的黑白化图片
     * @radios 颜色通道配置，依次为红、黄、 绿、 青、 蓝、紫六个通道
     */
    public static Bitmap createBlackWhiteImage(Bitmap image, float[] radios) {

        int width = image.getWidth();   //获取位图的宽
        int height = image.getHeight();  //获取位图的高

        Bitmap result = Bitmap.createBitmap(width, height, image.getConfig());

        int alpha = 0xff;
        int r = 0;
        int g = 0;
        int b = 0;
        int max = 0;
        int min = 0;
        int mid = 0;
        int gray = 0;

        float radioMax = 0;
        float radioMaxMid = 0;

        if (radios == null) {
            // 红        黄         绿         青         蓝        紫
            radios = new float[]{0.4f, 0.6f, 0.4f, 0.6f, 0.2f, 0.8f};
        }
        int[] resultPixle = new int[width * height];
        image.getPixels(resultPixle, 0, width, 0, 0, width, height);

        for (int i = 0; i < width; i++) {//一行行扫描
            for (int j = 0; j < height; j++) {

                gray = resultPixle[j * width + i];
//                gray = image.getPixel(i,j);//此方法效率极低，不要出现在循环体中，否则将导致极度耗时

                alpha = gray >>> 24;
                r = (gray >> 16) & 0x000000ff;
                g = (gray >> 8) & 0x000000ff;
                b = gray & 0x000000ff;


                if (r >= g && r >= b) {
                    max = r;
                    radioMax = radios[0];
                }
                if (g >= r && g >= b) {
                    max = g;
                    radioMax = radios[2];
                }
                if (b >= r && b >= g) {
                    max = b;
                    radioMax = radios[4];
                }
                if (r <= g && r <= b) { // g+ b = cyan 青色
                    min = r;
                    radioMaxMid = radios[3];
                }

                if (b <= r && b <= g) {//r+g = yellow 黄色
                    min = b;
                    radioMaxMid = radios[1];
                }
                if (g <= r && g <= b) {//r+b = m 洋红
                    min = g;
                    radioMaxMid = radios[5];
                }

                mid = r + g + b - max - min;

//              公式：gray= (max - mid) * ratio_max + (mid - min) * ratio_max_mid + min

                gray = (int) ((max - mid) * radioMax + (mid - min) * radioMaxMid + min);

                gray = (alpha << 24) | (gray << 16) | (gray << 8) | gray;

//                2000x3500大图，耗时相差2~5倍左右
//bitmap在循环中设置像素点，这个操作会导致耗时严重，耗时7秒。4096x4096图耗时22秒
//                result.setPixel(i, j, gray);
                resultPixle[j * width + i] = gray;//直接改变数组，最后bitmap再设像素
            }
        }

        result.setPixels(resultPixle, 0, width, 0, 0, width, height);//最后bitmap再设像素

        return result;
    }
}
